package com.jackrain.nea.fc.dl.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.dl.api.ViewDLPurRetToF4QueryCmd;
import com.jackrain.nea.fc.dl.mapper.ViewDLPurRetToF4Mapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.FastDateFormat;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author wuwenxue
 * @date 2018/05/22
 **/
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "dl")
public class ViewDLPurRetToF4QueryCmdImpl extends CommandAdapter implements ViewDLPurRetToF4QueryCmd {
    private final static FastDateFormat DATE_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd");

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {
        ValueHolder vh = new ValueHolder();
        ViewDLPurRetToF4Mapper viewDLPurRetToF4Mapper = ApplicationContextHandle.getBean(
                ViewDLPurRetToF4Mapper.class);
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(
                map.get("param"), "yyyy-MM-dd HH:mm:ss"), Feature.OrderedField);
        log.info("ViewDLPurRetToF4QueryCmdImpl--param-->{}",param);

        if (null == param || (!param.containsKey("IDS")) || param.getJSONArray("IDS").size() <= 0) {
            throw new NDSException("参数格式错误！");
        }

        Object[] ids = param.getJSONArray("IDS").toArray();
        String action = param.getString("action");
        JSONArray jsonArray = new JSONArray();

        if ("sendout".equals(action)||"submit".equals(action)) {
            //发出  提交
            List<HashMap> list = new ArrayList<>();
            if("sendout".equals(action)){
                list = viewDLPurRetToF4Mapper.selPurRetData(ids,2,null,null);
            }
            if("submit".equals(action)){
                list = viewDLPurRetToF4Mapper.selPurRetData(ids,null,2,null);
            }
            for (HashMap hashMap : list) {
                Long id = TypeUtils.castToLong(hashMap.get("ID"));
                String billNo = TypeUtils.castToString(hashMap.get("BILL_NO"));
                Date billDate = TypeUtils.castToDate(hashMap.get("BILL_DATE"));
                String formatBillDate = DATE_FORMAT.format(billDate);
                if("submit".equals(action)){
                    hashMap.put("AUTO_CHECK",1);
                    hashMap.put("AUTO_SEND",0);
                }
                if("sendout".equals(action)){
                    String carrier_name = TypeUtils.castToString(hashMap.get("CARRIER_NAME"));
                    String logistics_bill_no = TypeUtils.castToString(hashMap.get("LOGISTICS_BILL_NO"));
                    hashMap.put("AUTO_CHECK",1);
                    hashMap.put("AUTO_SEND",1);
                    hashMap.put("CARRIER_NAME",carrier_name);
                    hashMap.put("LOGISTICS_BILL_NO",logistics_bill_no);
                }
                hashMap.put("BILL_DATE", formatBillDate);

                if (null == hashMap.get("BILL_SOURCE")) {
                    hashMap.put("BILL_SOURCE", "FC");
                }

                if (null != hashMap.get("REFUND_TYPE")) {
                    Long billTypeId = TypeUtils.castToLong(hashMap.get("REFUND_TYPE"));
                    String billType = viewDLPurRetToF4Mapper.selbillType(billTypeId);
                    hashMap.put("REFUND_TYPE", billType);
                } else {
                    hashMap.put("REFUND_TYPE", null);
                }

                List<HashMap> itemList = viewDLPurRetToF4Mapper.selPurRetItemData(id);
                JSONArray itemArray = new JSONArray();
                for (HashMap itemH : itemList) {
                    itemH.put("BILL_NO", billNo);
                    //管理成本价
                    BigDecimal priceMgt = TypeUtils.castToBigDecimal(itemH.get("PRICE_MGT"));
                    //管理成本金额
                    BigDecimal amtMgt = TypeUtils.castToBigDecimal(itemH.get("AMT_MGT"));
                    //不含税采购价
                    BigDecimal priceNonTax = TypeUtils.castToBigDecimal(itemH.get("PRICE_NON_TAX"));
                    //不含税采购金额
                    BigDecimal amtNonTax = TypeUtils.castToBigDecimal(itemH.get("AMT_NON_TAX"));

                    if (null != priceMgt) {
                        itemH.put("PRICE_MGT", priceMgt);
                    } else {
                        itemH.put("PRICE_MGT", 0);
                    }

                    if (null != amtMgt) {
                        itemH.put("AMT_MGT", amtMgt);
                    } else {
                        itemH.put("AMT_MGT", 0);
                    }

                    if (null != priceNonTax) {
                        itemH.put("PRICE_NON_TAX", priceNonTax);
                    } else {
                        itemH.put("PRICE_NON_TAX", 0);
                    }

                    if (null != amtNonTax) {
                        itemH.put("AMT_NON_TAX", amtNonTax);
                    } else {
                        itemH.put("AMT_NON_TAX", 0);
                    }

                    JSONObject itemjs = new JSONObject(itemH);
                    itemArray.add(itemjs);
                }

                JSONObject js = new JSONObject(hashMap);
                js.put("DL_B_PUR_RET_ITEM", itemArray);
                JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(js,
                        "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
                jsonArray.add(param1);

            }
        } else if ("accept".equals(action)) {
            //验收
            List<HashMap> list = viewDLPurRetToF4Mapper.selPurRetData(ids,2,null,null);

            for (HashMap hashMap : list) {
                JSONObject unSumJson = new JSONObject();
                unSumJson.put("BILL_TYPE", "PUR_RET");
                unSumJson.put("BILL_NO", hashMap.get("BILL_NO"));
                unSumJson.put("STATUS_TYPE", "ACCEPT");
                unSumJson.put("OPR_ECODE", hashMap.get("MODIFIERNAME"));
                unSumJson.put("OPR_ENAME", hashMap.get("MODIFIERENAME"));
                unSumJson.put("OPR_DATE", hashMap.get("MODIFIEDDATE"));

                JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(unSumJson,
                        "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
                jsonArray.add(param1);
            }
          } else if("unsubmit".equals(action)||"void".equals(action)){
            //取消提交 作废
            List<HashMap> list = new ArrayList<>();
            if("unsubmit".equals(action)){
                list = viewDLPurRetToF4Mapper.selPurRetData(ids,null,1,null);
            }
            if("void".equals(action)){
                list = viewDLPurRetToF4Mapper.selPurRetData(ids,null,null,"N");
            }
            for (HashMap hashMap : list) {
                JSONObject unSubJson = new JSONObject();
                unSubJson.put("BILL_TYPE", "PUR_RET");
                unSubJson.put("BILL_NO", hashMap.get("BILL_NO"));
                if ("unsubmit".equals(action)) {
                    unSubJson.put("STATUS_TYPE", "UNCHK");
                }
                if ("void".equals(action)) {
                    unSubJson.put("STATUS_TYPE", "DEL");
                }
                unSubJson.put("OPR_ECODE", hashMap.get("MODIFIERNAME"));
                unSubJson.put("OPR_ENAME", hashMap.get("MODIFIERENAME"));
                unSubJson.put("OPR_DATE", hashMap.get("MODIFIEDDATE"));

                JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(unSubJson,
                        "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
                jsonArray.add(param1);
            }
        }
        vh.put("data", jsonArray);
        return vh;
    }
}
